<template>
  <div class="app-container">
    <el-table  border v-loading="loading" :data="secondaryList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="维护站编号" align="center" prop="wxCode" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="维护站名称" align="center" prop="wxUnit" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="企业类型" align="center" prop="enterpriseType" >
        <template slot-scope="scope">
          <span v-if="scope.row.enterpriseType === 1">维修企业</span>
          <span v-if="scope.row.enterpriseType === 2">营运车辆企业</span>
          <span v-if="scope.row.enterpriseType === 3">综合性能企业</span>
        </template>
      </el-table-column>
      <el-table-column label="维护站地址" align="center" prop="wxAddr" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="工位数" align="center" prop="workstation" />
      <el-table-column label="联系人" align="center" prop="wxPerson" />
      <el-table-column label="联系电话" align="center" prop="wxTel" />
      <el-table-column label="统一社会信用编码" width="130" align="center" prop="creditCode" />
      <el-table-column label="营业执照照片" align="center" prop="licensepic" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.licensepic"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="计量证书照片" align="center" prop="measurementTestingCertificate" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.measurementTestingCertificate"></image-preview>
        </template>
      </el-table-column>
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="法人电话" align="center" prop="legalTel" width="120"/>
      <el-table-column label="经营范围" align="center" prop="businessScope" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="区划编码" align="center" prop="orgCode" />
      <el-table-column label="所属区县" align="center" prop="orgName" width="180"/>
      <el-table-column label="站点状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status === 0">禁用</el-tag>
          <el-tag type="success" v-if="scope.row.status === 1">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否审核" align="center" prop="isApproval" >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isApproval === '0'">未审核</el-tag>
          <el-tag type="success" v-if="scope.row.isApproval === '1'">已审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核结果" align="center" prop="approvalResult">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.approvalResult === '0'">同意</el-tag>
          <el-tag type="danger" v-if="scope.row.approvalResult === '1'">不同意</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="approvalComments" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="禁用原因" align="center" prop="prohibitReason" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" width="180" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.isEdit === '1'"
            @click="handleSaveInfo(scope.row)"
          >完善站点信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="openSiteData"
      width="50%">
      <el-descriptions class="margin-top"   title="站点信息" :column="1" border>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            维护站编码
          </template>
          {{siteInfo.wxCode}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            维护站名称
          </template>
          {{siteInfo.wxUnit}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            维护站地址
          </template>
          {{siteInfo.wxAddr}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            联系人
          </template>
          {{siteInfo.wxPerson}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            联系电话
          </template>
          {{siteInfo.wxTel}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            所属区县
          </template>
          {{siteInfo.orgName}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            企业类型
          </template>
          {{siteInfo.enterpriseType}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            统一社会信用编码
          </template>
          {{siteInfo.creditCode}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            营业执照照片
          </template>
          <image-preview :src="siteInfo.licensepic" :width="50" :height="50"></image-preview>
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            法人
          </template>
          {{siteInfo.legalPerson}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            法人电话
          </template>
          {{siteInfo.legalTel}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            工位数
          </template>
          {{siteInfo.workstation}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            经营范围
          </template>
          {{siteInfo.businessScope}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            站点状态
          </template>
          {{siteInfo.status === 0 ? '禁用' : '启用'}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            禁用原因
          </template>
          {{siteInfo.prohibitReason}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            审核状态
          </template>
          {{siteInfo.isApproval === '0' ? '未审核' : '已审核'}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            审核结果
          </template>
          {{siteInfo.approvalResult === '0' ? '同意' : '不同意'}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            审核意见
          </template>
          {{siteInfo.approvalComments}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            编辑状态
          </template>
          {{siteInfo.isEdit === '0' ? '可编辑' : '不可编辑'}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            计量证书照片
          </template>
          <image-preview :src="siteInfo.measurementTestingCertificate" :width="50" :height="50"></image-preview>
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            计量证书开始日期
          </template>
          {{siteInfo.certificateStartDate}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            计量证书结束日期
          </template>
          {{siteInfo.certificateEndDate}}
        </el-descriptions-item>
        <el-descriptions-item :labelStyle="{'font-weight':'bold'}">
          <template slot="label">
            证书有效期(天)
          </template>
          {{siteInfo.certificatePeriodValidity}}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!-- 添加或修改汽车检测-二级维护站对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :before-close="dilogleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="维护站名称" prop="wxUnit">
          <el-input v-model="form.wxUnit"  maxlength="50" placeholder="请输入维护站名称" />
        </el-form-item>
        <el-form-item label="企业类型" prop="enterpriseType">
          <el-select  v-model="form.enterpriseType" placeholder="请选择企业类型" style="width: 100%;">
            <el-option  label="维修企业" :value="1"></el-option>
            <el-option  label="营运车辆企业" :value="2"></el-option>
            <el-option  label="综合性能企业" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护站地址" prop="wxAddr">
          <el-input v-model="form.wxAddr"  maxlength="100" placeholder="请输入维护站地址" />
        </el-form-item>
        <el-form-item label="工位数" prop="workstation">
          <el-input v-model="form.workstation"  maxlength="100" placeholder="请输入工位数" />
        </el-form-item>
        <el-form-item label="联系人" prop="wxPerson">
          <el-input v-model="form.wxPerson" placeholder="请输入联系人"  maxlength="20" />
        </el-form-item>
        <el-form-item label="联系电话" prop="wxTel">
          <el-input v-model="form.wxTel" placeholder="请输入联系电话"  maxlength="20" />
        </el-form-item>
        <el-form-item label="所属地区" required>
          <el-cascader v-model="form.orgCodes" :props="props" style="width: 100%" :options="options" :show-all-levels="true"></el-cascader>
        </el-form-item>
        <el-form-item label="统一社会信用编码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用编码"  maxlength="100" />
        </el-form-item>
        <el-form-item label="营业执照照片" required>
          <image-upload :limit="1" v-model="form.licensepic"></image-upload>
        </el-form-item>
        <el-form-item label="计量证书照片" required>
          <image-upload :limit="5" v-model="form.measurementTestingCertificate"></image-upload>
        </el-form-item>
        <el-form-item label="计量证书有效期" required>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.certificateStartDate" style="width: 100%;" value-format="yyyy-dd-MM"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.certificateEndDate" style="width: 100%;" value-format="yyyy-dd-MM"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="法人" prop="legalPerson">
          <el-input v-model="form.legalPerson" placeholder="请输入法人"  maxlength="20" />
        </el-form-item>
        <el-form-item label="法人电话" prop="legalTel">
          <el-input v-model="form.legalTel" placeholder="请输入法人电话"  maxlength="20" />
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope">
          <el-input type="textarea"  maxlength="200" show-word-limit :rows="4" v-model="form.businessScope" placeholder="请输入经营范围" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { siteInfo, getSecondary, delSecondary, addSecondary, updateSecondary, controlShow,updateStatus,wxUnitList } from "@/api/maintenanceenterprise/secondarysite";
import { getDistrictTreeList } from "@/api/system/district";
import { getToken } from "@/utils/auth";
export default {
  name: "Secondary",
  data() {
    return {
      openSiteData: false,
      siteInfo: {},
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      controlShow: false,
      props: {
        label: "name",
        value: "code",
        children: "chridrenList"
      },
      wxUnitList: [],
      options: [],
      fileList: [],
      accept: ".xls,.xlsx",
      importUrl: process.env.VUE_APP_BASE_API + "/vehicleservice/secondary/import/data",
      openImportData: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汽车检测-二级维护站表格数据
      secondaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wxCode: null,
        wxUnit: "",
        wxAddr: null,
        wxPerson: null,
        wxTel: null,
        creditCode: null,
        licensepic: null,
        legalPerson: null,
        legalTel: null,
        businessScope: null,
        orgCode: null,
        enterpriseType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wxUnit: [
          { required: true, message: '请输入维护站名称', trigger: 'blur' },
        ],
        wxAddr: [
          { required: true, message: '请输入维护站地址', trigger: 'blur' },
        ],
        wxPerson: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
        ],
        workstation: [
          { required: true, message: '请输入工位', trigger: 'blur' },
        ],
        certificateStartDate: [
          { required: true, message: '请输入证书开始日期', trigger: 'blur' },
        ],
        certificateEndDate: [
          { required: true, message: '请输入证书结束日期', trigger: 'blur' },
        ],
        wxTel: [
          { required: true, message: '请输入联系人电话', trigger: 'blur' },
        ],
        creditCode: [
          { required: true, message: '请输入社会统一代码', trigger: 'blur' },
        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' },
        ],
        legalTel: [
          { required: true, message: '请输入法人电话', trigger: 'blur' },
        ],
        businessScope: [
          { required: true, message: '请输入经营范围', trigger: 'blur' }
        ],
        enterpriseType: [
          {required: true, message: '请选择企业类型', trigger: 'change'}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDistrictTreeList();
    this.controlShowFuncation();
    this.getWxUnitList();
  },
  methods: {
    handleView(row){
      getSecondary(row.id).then(res => {
        this.siteInfo = res.data;
        this.openSiteData = true;
      })
    },
    handleSaveInfo(row){
      getSecondary(row.id).then(res => {
        this.form = res.data;
        this.form.orgCodes = res.data.orgCode;
        this.open = true;
      })
    },
    getWxUnitList(){
      wxUnitList().then(res => {
        this.wxUnitList = res.data
      })
    },
    dilogleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
            done();
        })
        .catch(_ => {});
    },
    updateStatus(e){
      updateStatus(e).then(res => {
        this.$message.success(res.msg);
      })
    },
    controlShowFuncation(){
      controlShow().then(res => {
        this.controlShow = res.data
      })
    },
    getDistrictTreeList(){
      getDistrictTreeList().then(res => {
        this.options = res.data
      })
    },
    handleClose(){
      this.fileList = []
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    uploadSuccess(response, file, fileList){
      if(response.code === 200){
        this.$message.success(response.msg);
        this.openImportData = false;
        this.fileList = fileList;
        this.getList();
      }else {
        this.$message.error(response.msg);
      }
    },

    openImport(){
      this.openImportData = true;
      this.fileList = [];
    },
    /** 查询汽车检测-二级维护站列表 */
    getList() {
      this.loading = true;
      siteInfo().then(response => {
        this.secondaryList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        wxCode: null,
        wxUnit: null,
        wxAddr: null,
        wxPerson: null,
        wxTel: null,
        creditCode: null,
        licensepic: null,
        legalPerson: null,
        legalTel: null,
        businessScope: null,
        orgCode: null,
        createTime: null,
        updateTime: null,
        measurementTestingCertificate: null,
        workstation: null,
        certificateStartDate: null,
        certificateEndDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.orgCode) {
        this.queryParams.orgCode = this.queryParams.orgCodes.toString();
      }

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加二级维护站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSecondary(id).then(response => {
        this.form = response.data;
        this.form.orgCodes = response.data.orgCode;
        this.open = true;
        this.title = "修改汽车检测-二级维护站";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orgCode = this.form.orgCodes.toString();
          if (this.form.id != null) {
            updateSecondary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSecondary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delSecondary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicleservice/secondary/export', {
        ...this.queryParams
      }, `secondary_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
